import React, {useEffect,useRef, useState} from 'react'
import {request} from 'umi'

interface intervalProps {
  noteCode: string,
  pullInterval?: number,
}

const useIntervalRequest = (props) => {
  const {noteCode} = props
  console.log(props)
  const ref = useRef<any>(null)
  const [loading,setLoading] = useState(false)
  const [result,setResult] = useState([]) 
   
  useEffect(() => {
    if (props.pullInterval && props.pullInterval < 0) return
    if(props.pullInterval) {
        ref.current = window.setInterval(() => {
        handleReqUserList()
      },props.pullInterval)
    } else {
        handleReqUserList()
    }

    return () => {
       if (props.pullInterval) {
          clearInterval(ref.current) 
       } 
    }
  },[])

  const handleReqUserList = () => {
    request('/api/user/list', {
      params: {
        page: 1,
        pageSize: 10,
      },
    }).then((res) => {
      if (res.err_code === 0) {
          setLoading(false)
          setResult(res.data)
      }
    });
  };

  return {
      result,
      loading
  }
}

//post api/dap/unify-query/query?noteCode=sydk_yx4bl_wnrzmbl_dg


export default useIntervalRequest

